package org.hackystat.iw.projectoverviewer.page.treemap;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.xml.datatype.XMLGregorianCalendar;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.hackystat.iw.projectoverviewer.ProjectOverviewerApplication;
import org.hackystat.iw.projectoverviewer.jaxb.projects.Project;
import org.hackystat.iw.projectoverviewer.page.BasePage;
import org.hackystat.iw.projectoverviewer.sensordata.SensorDataCollector;
import org.hackystat.utilities.tstamp.Tstamp;

/**
 * A page for displaying the treemap for a particular project.
 * 
 * @author Jianfei Liao
 * @author Nathan Britton
 */
public class TreeMapPage extends BasePage {
  
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Constructor. 
   * During the page rendering, collects sensor data, and initialize the applet.
   * 
   * @param param The page parameters. Needed for determine which project is being displayed.
   */
  public TreeMapPage(PageParameters param) {
    String host = null;
    String userEmail = null;
    String password = null;
    String svnUrl = null;
    
    String projectName = param.getString("projectName");
    add(new Label("projectName", projectName));
    
    // find the corresponding project instance for finding information we need
    ProjectOverviewerApplication app = (ProjectOverviewerApplication) this.getApplication();
    for (Project project : app.getProjects().getProject()) {
      if (projectName.equals(project.getProjectName())) {
        host = app.getSensorBaseHost();
        userEmail = project.getOwner();
        password = project.getPassword();
        svnUrl = project.getSvnUrl();
      }
    }
    
    String fileContent = "";
    try {
      // create new sensor data collector to update sensor data
      SensorDataCollector collector = new SensorDataCollector(host, userEmail, password);
      System.out.println("INFO: Retrieving data for project: " + projectName);
      collector.getRepository(svnUrl, projectName);
      collector.getOldSensorData(projectName);
      
      XMLGregorianCalendar endTime = Tstamp.makeTimestamp();
      XMLGregorianCalendar startTime = Tstamp.incrementHours(endTime, -1);
      collector.getSensorData(projectName, "FileMetric", startTime, endTime);
      collector.getSensorData(projectName, "Coverage", startTime, endTime);
      collector.getSensorData(projectName, "DevEvent", startTime, endTime);
      
      String userHome = System.getProperty("user.home");
      File tm3File = new File(userHome + "/.hackystat/projectoverviewer/" + projectName + ".tm3");
      File xmlFile = new File(userHome + "/.hackystat/projectoverviewer/" + projectName + ".xml");
      
      // update the project data file
      collector.write(tm3File, xmlFile);
    
      // read the project data file into a string
      FileReader fr = new FileReader(tm3File);
      StringBuffer fileData = new StringBuffer(1000);
      BufferedReader reader = new BufferedReader(fr);
      char[] buf = new char[1024];
      int numRead = 0;
      while ((numRead = reader.read(buf)) != -1) {
          fileData.append(buf, 0, numRead);
      }
      reader.close();
      fileContent = fileData.toString();
    }
    catch (Exception e) {
      System.out.println("*** Error while updating sensor data. ***");
      e.printStackTrace();
    }
    
    // replace some characters so they can be preserved while 
    // being fetch by the applet getParameter() method
    fileContent = fileContent.replaceAll("\t", "@TAB@");
    fileContent = fileContent.replaceAll("\n", "@NEWLINE@");
    fileContent = fileContent.replaceAll("\r\n", "@NEWLINE@");
    
    // dynamically create the param attribute for the applet tag
    String value = "value";
    WebMarkupContainer wmc = new WebMarkupContainer("fileContentID");
    wmc.add(new SimpleAttributeModifier(value, fileContent));
    add(wmc);
    
    wmc = new WebMarkupContainer("strategyID");
    wmc.add(new SimpleAttributeModifier(value, param.getString("strategy")));
    add(wmc);
    
    wmc = new WebMarkupContainer("colorProviderID");
    wmc.add(new SimpleAttributeModifier(value, param.getString("colorProvider")));
    add(wmc);
    
    wmc = new WebMarkupContainer("sizeID");
    wmc.add(new SimpleAttributeModifier(value, param.getString("size")));
    add(wmc);
    
    wmc = new WebMarkupContainer("colorID");
    wmc.add(new SimpleAttributeModifier(value, param.getString("color")));
    add(wmc);
  }
}
